/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.uit.ecommerce.weatherservice.servlets;

import com.uit.ecommerce.services.Exception_Exception;
import com.uit.ecommerce.services.ServiceLocation;
import com.uit.ecommerce.services.ServiceLocationService;
import com.uit.ecommerce.services.ServiceWeather;
import com.uit.ecommerce.services.ServiceWeatherService;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.ws.WebServiceRef;

@WebServlet(name = "servWeatherCity", urlPatterns = {"/servWeatherCity"})
public class servWeatherCity extends HttpServlet {
    @WebServiceRef(wsdlLocation = "WEB-INF/wsdl/localhost_8080/Weather_Webservice_Server/ServiceWeatherService.wsdl") 
                private ServiceWeatherService serviceweather;
    @WebServiceRef(wsdlLocation = "WEB-INF/wsdl/localhost_8080/Weather_Webservice_Server/ServiceLocationService.wsdl") 
       private ServiceLocationService serviceLocation;
    /** 
     * 
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException 
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, Exception_Exception {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        HttpSession session = request.getSession();
        PrintWriter out = response.getWriter();
        String action=request.getParameter("action");
        try {
            if(action.equalsIgnoreCase("view")){
               viewWeatherCity(response, session);
             }
            else if(action.equalsIgnoreCase("search")){
               searchWeatherCity(request, response, session);
            }
            else if(action.equalsIgnoreCase("loadcity")){
              loadcity(request, response, session);
            }
            else if(action.equalsIgnoreCase("future")){
                ViewCityWeather3Day(request, response, session);
            }
        } finally {            
            out.close();
        }
    }
    private void ViewCityWeather3Day(HttpServletRequest request, HttpServletResponse response,HttpSession session) throws Exception_Exception, IOException{
                ServiceWeather ws=serviceweather.getServiceWeatherPort();
                String cityName=request.getParameter("cityName");
                String cityCode=request.getParameter("cityCode");
                DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
                java.util.Date date = new java.util.Date();
                String today=dateFormat.format(date);
                String aftertoday=dateFormat.format(new Date(date.getYear(), date.getMonth(), date.getDate()+1));
                String aftertoday1=dateFormat.format(new Date(date.getYear(), date.getMonth(), date.getDate()+2));   
                com.uit.ecommerce.services.WeatherCityDTO weathertoday=(com.uit.ecommerce.services.WeatherCityDTO) ws.getWeatherCityByDateAndName(today, cityName);
                session.setAttribute("today", weathertoday);
                com.uit.ecommerce.services.WeatherCityDTO weatheraftertoday=(com.uit.ecommerce.services.WeatherCityDTO) ws.getWeatherCityByDateAndName(aftertoday, cityName);
                session.setAttribute("aftertoday", weatheraftertoday);
                com.uit.ecommerce.services.WeatherCityDTO weatheraftertoday1=(com.uit.ecommerce.services.WeatherCityDTO) ws.getWeatherCityByDateAndName(aftertoday1, cityName);
                session.setAttribute("aftertoday1", weatheraftertoday1);
                com.uit.ecommerce.services.WeatherCityDTO mintemp=(com.uit.ecommerce.services.WeatherCityDTO) ws.getWeatherCityMinTemp(cityCode);
                session.setAttribute("mintemp", mintemp);
                com.uit.ecommerce.services.WeatherCityDTO maxtemp=(com.uit.ecommerce.services.WeatherCityDTO) ws.getWeatherCityMaxTemp(cityCode);
                session.setAttribute("maxtemp", maxtemp);
                String path = "./jsps/ThoiTietTp3Ngay.jsp";
               response.sendRedirect(path);
}
    private void loadcity(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception_Exception, IOException {
         String areaID=request.getParameter("area");
         ServiceLocation ls=serviceLocation.getServiceLocationPort();
         ArrayList<com.uit.ecommerce.services.CityDTO> list=(ArrayList<com.uit.ecommerce.services.CityDTO>) ls.getAllCityByAreaCode(areaID);
        for(int i=0;i<list.size();i++){
             response.getWriter().println("<option value='"+list.get(i).getName()+"'>"+list.get(i).getName()+"</option>");
        }
    }
private void searchWeatherCity(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception_Exception, IOException{
               DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
                java.util.Date date = new java.util.Date();
                String today=dateFormat.format(date);
               String city=request.getParameter("city");
               ServiceWeather ws=serviceweather.getServiceWeatherPort();
               com.uit.ecommerce.services.WeatherCityDTO cityweather= (com.uit.ecommerce.services.WeatherCityDTO) ws.getWeatherCityByDateAndName(today, city);
               response.getWriter().println("<tr><td>Thông tin thời tiết</td><td><a href='../servWeatherCity?action=future&cityCode="+cityweather.getCityId()+"&cityName="+cityweather.getCityName()+"'>"+cityweather.getCityName()+"</a></td><td></td><td></td></tr>");
              response.getWriter().println("<tr><td>Nhiệt độ thấp nhất</td><td>Nhiệt độ cao nhất</td><td>Thông tin</td><td>Thời tiết</td></tr>");
              String weather="<tr><th>"+cityweather.getTempMin()+"</th><th>"+cityweather.getTempMax()+"</th><th>"+cityweather.getNote()+"</th>";
              if("Nắng".equals(cityweather.getWeather())||"Nắng".equals(cityweather.getWeather()))
                  weather+="<th><img src='../imgs/weather/TroiNang.gif'></th>";
              else if("Mưa".equals(cityweather.getWeather()))
                       weather+=" <th><img src='../imgs/weather/CoMua.gif'></th>";
                   else if("Lạnh".equals(cityweather.getWeather()))
                         weather+="<th><img src='../imgs/weather/TroiRet.gif'></th>";
                          else if("null".equals(cityweather.getWeather()))
                                weather+="<th>null</th>";
                                else if("Giông".equals(cityweather.getWeather()))
                                    weather+="<th><img src='../imgs/weather/MuaRaoVaGiong.gif'></th>";
                                       else
                                       weather+="<th><img src='../imgs/weather/NhieuMayKhongMua.gif'></th>";
               weather+="</tr>";
              response.getWriter().println(weather);
}
private void viewWeatherCity(HttpServletResponse response, HttpSession session) throws Exception_Exception, IOException{
              DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
                java.util.Date date = new java.util.Date();
                String today=dateFormat.format(date);
                ServiceWeather ws=serviceweather.getServiceWeatherPort();
               ServiceLocation ls=serviceLocation.getServiceLocationPort();
               com.uit.ecommerce.services.WeatherCityDTO HaNoi= (com.uit.ecommerce.services.WeatherCityDTO) ws.getWeatherCityByDateAndName(today, "Hà Nội");
               session.setAttribute("HaNoi", HaNoi);
               com.uit.ecommerce.services.WeatherCityDTO DNA= (com.uit.ecommerce.services.WeatherCityDTO) ws.getWeatherCityByDateAndName(today, "Đà Nẵng");
               session.setAttribute("DNA", DNA);
               com.uit.ecommerce.services.WeatherCityDTO HCM= (com.uit.ecommerce.services.WeatherCityDTO) ws.getWeatherCityByDateAndName(today, "Hồ Chí Minh");
               session.setAttribute("HCM", HCM);
               ArrayList<com.uit.ecommerce.services.AreaDTO> area=(ArrayList<com.uit.ecommerce.services.AreaDTO>) ls.getAllArea();
               session.setAttribute("area", area);
                String path = "./jsps/ThoiTietTinhThanh.jsp";
                response.sendRedirect(path);
}
    /** 
     * 
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException 
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (Exception_Exception ex) {
            Logger.getLogger(servWeatherCity.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * 
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException 
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (Exception_Exception ex) {
            Logger.getLogger(servWeatherCity.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * 
     * @return 
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }
}
